﻿using System;
using CRC.GestionSalones.Dominio.Clases;
using NHibernate;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Collections;
using CRC.Comun.Presentacion;

namespace CRC.GestionSalones.Controladores
{
    public class ControladorGestionFotosSalon: ControladorGestionCategorias
    {
        private Salon salon = null;

        private static String strQryFotosCat = 
            "select f.Id, f.Nombre, f.miniatura, f.Comentario from Foto f " +
            "left join f.Salon " + 
            "where f.Salon.Id = :IdSalon and f.Categoria.Id = :IdCat";
        private static String strQryElimFotoDeInc = "delete from salones_fotos_incidencia where foto_id=:id";
        private static String strQryElimFoto = "delete from salones_fotos where id=:id";
        
        public void obtenerSalon(int pIntSalon)
        {
            ISession session = null;
            try
            {
                session = NHibernate.NHibernateHelper.GetCurrentSession();
                this.salon = (Salon)session.Get(typeof(Salon), pIntSalon);
            }
            catch (HibernateException he)
            {
                if (session != null) session.Close();
                throw he;
            }
        }

        /// <summary>
        ///  Da los id, la miniatura y el nombre de las fotos del salon
        ///  con la categoria pIntCat
        /// </summary>
        /// <param name="pIntCat">Identificador de la categoria</param>
        public IList<Object[]> listarFotos(int pIntCat) 
        {
            ISession session = null;
            IList<Object[]> res = null, aux = null;
            try
            {
                session = NHibernate.NHibernateHelper.GetCurrentSession();

                IQuery qry = session.CreateQuery(strQryFotosCat);
                qry.SetParameter("IdCat", pIntCat);
                qry.SetParameter("IdSalon", salon.Id);

                aux = qry.List<Object[]>();

                res = new List<Object[]>();
                foreach (Object[] f in aux) 
                {
                    Byte[] img = (Byte[])f[2];
                    res.Add(new Object[] { f[0], f[1], Image.FromStream(new MemoryStream(img)), f[3], (img.Length / 1024)});
                }

                return res;
            }
            catch (HibernateException he)
            {
                if (session != null) session.Close();
                throw he;
            }
        }

        public Object[] nuevaFoto(String pStrRuta, int pIntCat, String pStrCom)
        {

            ISession session = null;
            ITransaction tx = null;
            try
            {
                session = NHibernate.NHibernateHelper.GetCurrentSession();
                tx = session.BeginTransaction();
                Categoria cat = (Categoria)session.Get(typeof(Categoria), pIntCat);
                Foto f = salon.nuevaFoto(pStrRuta,
                    cat,
                    pStrCom);

                session.Flush();
                tx.Commit();

                return new Object[] { f.Id, f.Miniatura(), f.Nombre, f.Comentario, f.TamañoEnKB};
            }
            catch (HibernateException he)
            {
                if (tx != null) tx.Rollback();
                if (session != null) session.Close();
                throw he;
            }
        }

        public Image verFoto(int pIntFoto)
        {
            ISession session = null;
            try
            {
                session = NHibernate.NHibernateHelper.GetCurrentSession();
                Foto f = (Foto)session.Get(typeof(Foto), pIntFoto);
                return f.Imagen();
            }
            catch (HibernateException he)
            {
                if (session != null) session.Close();
                throw he;
            }
        }

        public void eliminarFoto(int pIntFoto) 
        {
            ISession session = null;
            ITransaction tx = null;
            try
            {
                session = NHibernate.NHibernateHelper.GetCurrentSession();
                tx = session.BeginTransaction();
                IQuery sqlQry = session.CreateSQLQuery(strQryElimFotoDeInc).SetParameter("id", pIntFoto);
                sqlQry.ExecuteUpdate();

                sqlQry = session.CreateSQLQuery(strQryElimFoto).SetParameter("id", pIntFoto);
                sqlQry.ExecuteUpdate();
                
                session.Flush();
                tx.Commit();
            }
            catch (HibernateException he)
            {
                if (tx != null) tx.Rollback();
                if (session != null) session.Close();
                throw he;
            }
        }
    }
}
